import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '登录',
    component: () => import('../views/LoginView')
  },
  {
    path: '/home',
    name: 'home',
    meta: { isAuth: true },
    component: () => import('../views/ManageView.vue'),
    children: [
      {
        path: 'admin',
        name: '管理员管理',
        component: () => import('../components/user/AdminView.vue')
      },
      {
        path: 'user',
        name: '用户管理',
        component: () => import('../components/user/UserVIew.vue')
      },
      {
        path: 'animal',
        name: '动物管理',
        component: () => import('../components/pet/PetView.vue')
      },
      {
        path: 'add',
        name: '宠物新增',
        component: () => import('../components/pet/AddPet.vue')
      },
      {
        path: 'addGoods',
        name: '宠物新增',
        component: () => import('../components/goods/AddGood.vue')
      },
      {
        path: 'comment',
        name: '评论管理',
        component: () => import('../components/comment/CommentView.vue')
      },
      {
        path: 'activity',
        name: '活动管理',
        component: () => import('../components/activity/ActivityView.vue')
      },
      {
        path: 'adopt',
        name: '领养管理',
        component: () => import('../components/adopt/AdoptView.vue'),
        redirect: '/home/adopt/process',
        children: [
          {
            path: 'process',
            name: '审核',
            component: () => import('../components/adopt/ProcessView.vue')
          },
          {
            path: 'allprocess',
            name: '所有审核',
            component: () => import('../components/adopt/AllProcess.vue')
          }
        ]
      },
      {
        path: 'goods',
        name: '商品管理',
        component: () => import('../components/goods/GoodsView.vue')
      }
    ]
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    const admin = JSON.parse(sessionStorage.getItem('user'))
    if (admin == null) {
      next('/')
    } else {
      next()
    }
  } else {
    next()
  }
})

router.afterEach((to, from) => {
  document.title = to.name
})
export default router
